/**
 * Copyright (C) 2009 Ignasi Fosch Alonso
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 
 * details.
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * You can reach me by mail at natx@y10k.ws
 * You can reach him by paper mail at:
 * C. LLEIDA, 41-43, 3r 1a
 * El Prat de Llobregat 08820
 * Barcelona, SPAIN
 */
package ws.y10k.vi;

import java.rmi.RemoteException;

import com.vmware.vim25.FileFault;
import com.vmware.vim25.InsufficientResourcesFault;
import com.vmware.vim25.InvalidProperty;
import com.vmware.vim25.InvalidState;
import com.vmware.vim25.RuntimeFault;
import com.vmware.vim25.TaskInProgress;
import com.vmware.vim25.VirtualMachinePowerState;
import com.vmware.vim25.VmConfigFault;
import com.vmware.vim25.mo.VirtualMachine;

/**
 * @author natx
 *
 */
public class Guest implements TreeLeaf {
	
	private VirtualMachine vm;
	private TreeLeaf parent;

	public void start() throws VmConfigFault, TaskInProgress, FileFault, InvalidState, InsufficientResourcesFault, RuntimeFault, RemoteException {
		this.vm.powerOnVM_Task(((Host) parent).hs);
	}

	public boolean isSuspended() {
		return this.vm.getRuntime().getPowerState() == VirtualMachinePowerState.suspended;
	}
	
	public boolean isPoweredOn() {
		return this.vm.getRuntime().getPowerState() == VirtualMachinePowerState.poweredOn;
	}

	@Override
	public String getName() {
		return vm.getName();
	}

	/* (non-Javadoc)
	 * @see ws.y10k.vi.TreeLeaf#getChildren()
	 */
	@Override
	public Object[] getChildren() throws InvalidProperty, RuntimeFault,
			RemoteException {
		// TODO Z: Check why this should happen
		return null;
	}

	/* (non-Javadoc)
	 * @see ws.y10k.vi.TreeLeaf#getParent()
	 */
	@Override
	public TreeLeaf getParent() {
		return parent;
	}

	/* (non-Javadoc)
	 * @see ws.y10k.vi.TreeLeaf#hasChildren()
	 */
	@Override
	public boolean hasChildren() {
		// TODO Z: Check why this should happen
		return false;
	}

	/* (non-Javadoc)
	 * @see ws.y10k.vi.TreeLeaf#setParent(ws.y10k.vi.TreeLeaf)
	 */
	@Override
	public void setParent(TreeLeaf parent) {
		this.parent = parent; 
	}
	
	public Guest(VirtualMachine vm, TreeLeaf parent) {
		this.vm = vm;
		this.setParent(parent);
	}

}
